<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/10/19
 * Time: 15:58
 */

namespace app\index\controller;

use app\index\base_controller\BaseController;
use app\index\table\FurnTable;
use think\Db;
use think\Exception;
use app\index\table\ConFurnTable;

class ConFurn extends BaseController
{
    public function furnList() {
        return $this->fetch('furn_list');
    }

    public function getFurnList() {
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'F.FURN_NO desc';
        }

        $condition = ConFurnTable::getSessionConditions();
        try{
            $listSql = ConFurnTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = ConFurnTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    public function addPage() {
        $this->assign('data',null);
        $this->view->engine->layout('layout_popup');
        return $this->fetch('furn_add');
    }

    public function addFurn() {
        $data = input();
        try {
            ConFurnTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);

        } catch (Exception $e) {
            return json_encode(['code'=>'0','msg'=>'操作失败：'.$e->getMessage()]);
        }
    }

    public function editPage() {
        $furnNo = input('furnNo');
        $data = ConFurnTable::get($furnNo);
        if(empty($data)){
            return '查询家具信息失败';
        }
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return $this->fetch('furn_edit');
    }

    public function editFurn() {
        $furnNo = input('FURN_NO');

        $old = ConFurnTable::get($furnNo);
        if (empty($old)) {
            return json_encode(['code'=>'0','msg'=>'查询家具信息失败','data'=>input()]);
        }

        $data = input();
        try {
            ConFurnTable::update(['FURN_NO'=>$furnNo],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);

        } catch (Exception $e) {
            return json_encode(['code'=>'0','msg'=>'操作失败：'.$e->getMessage()]);
        }
    }

    /**
     * 批量删除
     */
    public function deleteAll(){
        $list = input('list');
        Db::startTrans();
        try{
            foreach ($list as $furnNo){
                ConFurnTable::delete(['FURN_NO'=>$furnNo]);
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }



    /**
     * page--高级查询
     */
    public function searchPage(){
        $this->assign('data',null);
        $this->view->engine->layout('layout_popup');
        return view('furn_search');
    }


    /**
     * 转固
     */
    public function toFix() {
        $id = input('id');
        $data = Db::table('CON_FURN')
            ->where(['FURN_NO'=>$id])
            ->find();
        if (empty($data)) {
            return json_encode(['code'=>'0','msg'=>'查询资产信息失败']);
        }
        if ($data['IS_FIXED'] == '1') {
            return json_encode(['code'=>'0','msg'=>'该资产已转固']);
        }
        Db::startTrans();

        try {
            $updateBuiData = $data;
            unset($updateBuiData['CONS_ID']);
            unset($updateBuiData['IS_FIXED']);
            unset($updateBuiData['FIXED_NO']);
            unset($updateBuiData['FIXED_DATE']);
            unset($updateBuiData['FIXED_USER_ID']);
            unset($updateBuiData['INPUT_PERSON']);
            unset($updateBuiData['INPUT_TIME']);
            unset($updateBuiData['NUMROW']);
            $buildingNo = FurnTable::add($updateBuiData);

            $updateConData['IS_FIXED'] = 1;
            $updateConData['FIXED_NO'] = $buildingNo;
            $updateConData['FIXED_DATE'] = date('Y-m-d');
            $updateConData['FIXED_USER_ID'] = Login::getUserId();
            ConFurnTable::update(['FURN_NO'=>$id],$updateConData);
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'已转固']);
        } catch (Exception $e) {
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }


    /**
     * 打印
     */
    public function printPage() {
        $id = input('id');
        $data = ConFurnTable::get($id);
        if(empty($data)){
            return '查询家具信息失败';
        }
        $config = ConFurnTable::getConfigPrint();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('furn_print');
    }

    public function addConditionSession() {
        $condition = input();
        Login::setListConditionConFurn($condition);
        return json_encode(['code'=>'200']);
    }

    public function importPage() {
        $this->view->engine->layout('layout_popup');
        return view('import');
    }

    public function import() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = ConFurnTable::FURN_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            try{
                $constructionData = Db::table('CONSTRUCTION_INFO')->where(['CONS_NO'=>$data['CONS_NO']])->find();
                if (empty($constructionData)) {
                    Db::rollback();
                    return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错,所属在建工程编号错误"]);
                }
                unset($data['CONS_NO']);
                $data['CONS_ID'] = $constructionData['CONS_ID'];

                ConFurnTable::addImport($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }

    /**
     * page 家具导出页面
     */
    public function exportExcelPage() {
        $condition = ConFurnTable::getSessionConditions();

        try{
            $totalSql = ConFurnTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);

            $arr = $this->getDownLoadList($total);
            $this->assign('downList',$arr);

            $this->view->engine->layout('layout_popup');
            return view('export_excel');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * action 家具导出Excel
     */
    public function exportExcel(){
        $sortStr = 'F.FURN_NO desc';
        $start = input('start');
        $startL = $start-1;
        $end = input('end');
        $endL = $end-$start+1;

        $condition = ConFurnTable::getSessionConditions();

        try{
            $listSql = ConFurnTable::table()->where($condition)->limit($startL,$endL)->order($sortStr)->fetchSql()->select();
            $list = Db::query($listSql);
            ConFurnTable::exportExcel($list,"在建工程家具$start-$end");
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }



}